import 'package:elevator/get/intl_keys.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'dart:io';
import 'package:ftpconnect/ftpConnect.dart';
import 'package:path_provider/path_provider.dart';

import '../../../../../../model/build_info.dart';
import '../../../../../../model/lift_info.dart';

class SpecsLogic extends GetxController {

  BuildInfo?  buildInfo;
  LiftInfo? liftInfo;

  var imgPath = "".obs;

  var msg = "".obs;

  
  @override
  void onReady() {
    super.onReady();
    
    loadImg();
  }
  
  loadImg() async {
    if(null!=buildInfo && null !=liftInfo){
      String toDownloadFile = "img/specs/build_${buildInfo?.buildId}/LiftAdr_${liftInfo?.order}.jpg";
      debugPrint('SpecsLogic:准备下载图片：$toDownloadFile');
      msg.value = IntlKeys.startLoadingImage.tr;
      Directory directory = await getTemporaryDirectory();
      String path = '${directory.path}${Platform.pathSeparator}img${Platform.pathSeparator}specs${Platform.pathSeparator}build_${buildInfo?.buildId}';
      debugPrint('SpecsLogic:本地目录 $path');
      String localFile = "$path${Platform.pathSeparator}LiftAdr_${liftInfo?.order}.jpg";
      var fileExist = File(localFile).existsSync();
      if(fileExist){
        debugPrint('SpecsLogic:图片已经存在，开始加载本地图片');
        msg.value = IntlKeys.imageLoading.tr;
        imgPath.value = localFile;
        return;
      }
      var dir = Directory(path);
      var exist = dir.existsSync();
      if (exist) {
        debugPrint('SpecsLogic:当前文件夹已经存在');
        doDownload( toDownloadFile, localFile);
      } else {
        var result = await dir.create(recursive :true)
            .then((value) => doDownload(toDownloadFile,localFile))
            .catchError((onError){
              msg.value = IntlKeys.imageLoadWithWrongPath.tr;
              debugPrint('SpecsLogic:$onError');
            });
        debugPrint('SpecsLogic:result:$result');
      }

    }
  }

  doDownload(String toDownloadFile,String localFile){
    msg.value = IntlKeys.imageDownloading.tr;

    downloadFile(toDownloadFile,localFile)
        .then((value) => {
          debugPrint("SpecsLogic:$value"),
          msg.value = IntlKeys.imageDownloadToLoad.tr,
          imgPath.value = localFile
        })
        .onError((error, stackTrace) => {
          debugPrint("SpecsLogic:$error"),
          msg.value = IntlKeys.imageDownloadingErrorTips.tr,
        }
    );
  }

  Future<bool> downloadFile(String toDownloadFile,String localFile) async{
    FTPConnect ftpConnect = FTPConnect('36.141.45.30',port: 58021,user:'admin', pass:'adminemqx123');
    String fileName = toDownloadFile;
    debugPrint("SpecsLogic:start connect");
    await ftpConnect.connect();
    bool res = await ftpConnect.downloadFileWithRetry(fileName, File(localFile));
    debugPrint("SpecsLogic:end connect:res$res");
    await ftpConnect.disconnect();
    debugPrint("SpecsLogic:ftpConnect.disconnect");
    return res;
  }

}
